/**
 * License Agreement.
 *
 * Rich Faces - Natural Ajax for Java Server Faces (JSF)
 *
 * Copyright (C) 2007 Exadel, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */
package calendar;

import bean.RegisteredUserBean;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

import javax.faces.event.ValueChangeEvent;
import manager.EventManager;

import org.richfaces.model.CalendarDataModel;
import org.richfaces.model.CalendarDataModelItem;

/**
 * @author Ilya Shaikovsky
 *
 */
@ManagedBean(name = "calendarData")
@RequestScoped
public class CalendarDataModelImpl implements CalendarDataModel {

    /* (non-Javadoc)
     * @see org.richfaces.component.CalendarDataModel#getData(java.util.Date[])
     */
    private CalendarDataModelItem[] items;
    private String currentDescription;
    private String currentShortDescription;
    private Date currentDate;
    private boolean currentDisabled;
    @ManagedProperty("#{registeredUserBean}")
    protected RegisteredUserBean rub;


    /* (non-Javadoc)
     * @see org.richfaces.model.CalendarDataModel#getData(java.util.Date[])
     */
    public CalendarDataModelItem[] getData(Date[] dateArray) {
        if (dateArray == null) {
            return null;
        }
        if (items == null) {
            items = new CalendarDataModelItem[dateArray.length];
            for (int i = 0; i < dateArray.length; i++) {
                items[i] = createDataModelItem(dateArray[i]);
            }
        }
        return items;
    }

    /**
     * @param date
     * @return CalendarDataModelItem for date
     */
    protected CalendarDataModelItem createDataModelItem(Date date) {
        CalendarDataModelItemImpl item = new CalendarDataModelItemImpl();
        EventManager evm = new EventManager();

        Calendar c = Calendar.getInstance();
        c.setTime(date);
        item.setDay(c.get(Calendar.DAY_OF_MONTH));

        item.setEnabled(true);
        item.setStyleClass("rel-hol");
        item.setEvents(evm.getUserEventsForDay(rub.getRegisteredUser(), date));
        return item;
    }

    /* (non-Javadoc)
     * @see org.richfaces.model.CalendarDataModel#getToolTip(java.util.Date)
     */
    public Object getToolTip(Date date) {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * @return items
     */
    public CalendarDataModelItem[] getItems() {
        return items;
    }

    /**
     * @param setter for items
     */
    public void setItems(CalendarDataModelItem[] items) {
        this.items = items;
    }

    /**
     * @return currentDescription
     */
    public String getCurrentDescription() {
        return currentDescription;
    }

    /**
     * @param currentDescription
     */
    public void setCurrentDescription(String currentDescription) {
        this.currentDescription = currentDescription;
    }

    /**
     * @return currentDisabled
     */
    public boolean isCurrentDisabled() {
        return currentDisabled;
    }

    /**
     * @param currentDisabled
     */
    public void setCurrentDisabled(boolean currentDisabled) {
        this.currentDisabled = currentDisabled;
    }

    /**
     * @return currentShortDescription
     */
    public String getCurrentShortDescription() {
        return currentShortDescription;
    }

    /**
     * @param currentShortDescription
     */
    public void setCurrentShortDescription(String currentShortDescription) {
        this.currentShortDescription = currentShortDescription;
    }

    /**
     * @return currentDate
     */
    public Date getCurrentDate() {
        return currentDate;
    }

    /**
     * @param currentDate
     */
    public void setCurrentDate(Date currentDate) {
        this.currentDate = currentDate;
    }

    public RegisteredUserBean getRub() {
        return rub;
    }

    public void setRub(RegisteredUserBean rub) {
        this.rub = rub;
    }
}
